package top.jle.spring.basic.fis.validators;


import top.jle.jpa.basic.entities.UnDeletableEntity;
import top.jle.spring.basic.domain.BasicEntity;
import top.jle.jpa.basic.exception.BasicExceptionCode;
import top.jle.jpa.basic.util.ExceptionUtils;

import java.util.function.Supplier;

public interface EntityValidator{

    static <T extends BasicEntity> void exists(T t){
        exists(t, ExceptionUtils.build(BasicExceptionCode.DATA_NOT_EXISTS));
    }

    static <T extends BasicEntity>  void valid(T t) {
       valid(t, ExceptionUtils.buildForInvalidParam());
    }
    static <T extends BasicEntity,E extends RuntimeException>  void valid(T t,Supplier<E> supplier) {
        if (t==null || !t.valid()) {
            throw supplier.get();
        }
    }
    static  <T extends BasicEntity,E extends RuntimeException> void exists(T t, Supplier<E> supplier){
        if (t==null || t.getStatus()!= UnDeletableEntity.STATUS_VALID) {
            throw supplier.get();
        }
    }
}
